<?php
namespace Api\Controller;
use Common\Model\EditorModel;
use Common\Model\GameModel;
use Common\Model\PayModel;
use Common\Model\PornModel;
use Common\Model\RedpackModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Common\Model\WeixinModel;
use Think\Controller;
class IndexController extends Controller {
    const SUCCESS   =0;//成功
    const FAILURE   =1;//失败

    //微信支付回调验证
    public function index(){
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];

        // 这句file_put_contents是用来查看服务器返回的XML数据 测试完可以删除了
        //file_put_contents(WEB_ROOT.'/log2.txt',$xml,FILE_APPEND);

        //将服务器返回的XML数据转化为数组
        $data = PayModel::xml2array($xml);
        // 保存微信服务器返回的签名sign
        $data_sign = $data['sign'];
        // sign不参与签名算法
        unset($data['sign']);
        $sign = PayModel::makeSign($data);

        $var=var_export($data,true);
        //file_put_contents(WEB_ROOT.'/log2.txt',$var,FILE_APPEND);

        //file_put_contents(WEB_ROOT.'/log2.txt',$sign.'____'.$data_sign,FILE_APPEND);

        // 判断签名是否正确  判断支付状态
        if ( ($sign===$data_sign) && ($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS') ) {
            $result = $data;
            //获取服务器返回的数据
            $order_sn = $data['out_trade_no'];			//订单单号
            $openid = $data['openid'];					//付款人openID
            $total_fee = $data['total_fee'];			//付款金额
            $transaction_id = $data['transaction_id']; 	//微信支付流水号

           // file_put_contents(WEB_ROOT.'/log2.txt','in-----',FILE_APPEND);
            //更新数据库
            $this->updateOrderPay($order_sn,$openid,$total_fee,$transaction_id);

        }else{
            $result = false;
        }
        // 返回状态给微信服务器
        if ($result) {
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        }else{
            $str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }
        echo $str;
        return $result;
    }
    private function updateOrderPay($order_sn,$openid,$total_fee,$transaction_id){
        list($type,$orderId,$time,$rnd)=explode('_',$order_sn);
        $game=GameModel::getGameInstance()->where('gid='.$orderId)->find();
        switch ($type){
            case 'w'://游戏对赌
                GameModel::getJoinInstance()->where('jid='.$orderId)->save(
                    array(
                        'hasPay'=>1,
                        'transaction_id'=>$transaction_id,
                        'total_fee'=>$total_fee,
                        'out_trade_no'=>$order_sn
                    )
                );
                break;
            case 'g'://新建游戏
                if($game['hasPay']==1){
                    file_put_contents(WEB_ROOT.'/log2.txt',"{$order_sn},{$openid},{$total_fee},{$transaction_id}\r\n",FILE_APPEND);
                    return false;
                }
                GameModel::getGameInstance()->where('gid='.$orderId)->save(
                    array(
                        'hasPay'=>1,
                        'transaction_id'=>$transaction_id,
                        'total_fee'=>$total_fee,
                        'out_trade_no'=>$order_sn
                    )
                );
                RedpackModel::createRecord($game['gid'],$game['totalPrize'],$game['packNum'],$game['packMin']);
                break;
        }
    }



    public function checkPayStatus(){
        $id=I('post.id',0,'intval');
        $type=I('post.type');
        if(!$id){
            $this->ajaxReturn(array('status'=>1,'msg'=>'参数错误'));
        }
        switch ($type){
            case 'w'://对赌保证金
                $hasPay=GameModel::getJoinInstance()->where('jid='.$id)->getField('hasPay');
                break;
            case 'g'://创建游戏
                $hasPay=GameModel::getGameInstance()->where('gid='.$id)->getField('hasPay');
                break;
            default:
                $this->ajaxReturn(array('status'=>1,'msg'=>'参数类型错误'));
                break;
        }
        $this->ajaxReturn(array('status'=>0,'msg'=>'success','payStatus'=>$hasPay));
    }




    public function ajaxUpload(){
        $from = I('get.tp');
        $res = UtilsModel::imageUpload('file',FILE_UPLOAD_DIR);

        if($from && $from=='group') {
            $result = UtilsModel::image_rotate($res['msg']);
            if ($result['status']!=0){
                $this->ajaxReturn($result);
            }
            $res['msg']=$result['msg'];
        }
        if($from && ($from=='user' || $from=='group')) {
            $id = $res['id'];
            $audit = M('photo_lib', null, DB_MAIN_CFG)->where('id=' . $id)->getField('audit');
            switch ($audit) {
                case PHOTO_AUDIT_UNPASS:
                case PHOTO_AUDIT_PASS:
                case PHOTO_AUDIT_REVIEW:
                    break;
                default:
                    $audit = PornModel::checkPic($res['msg']);
                    M('photo_lib', null, DB_MAIN_CFG)->where('id=' . $id)->setField('audit', $audit);
                    break;
            }
            switch ($audit) {
                case PHOTO_AUDIT_UNPASS:
                    $ret = array('status' => 1, 'msg' => '包含色情图片请调整', 'pic' => '');
                    break;
                case PHOTO_AUDIT_PASS:
                    $ret = array('status' => 0, 'msg' => '上传成功', 'pic' => $res['msg']);
                    break;
                case PHOTO_AUDIT_REVIEW:
                    $ret = array('status' => 0, 'msg' => '可能包含色情图片，需要进行人工审核', 'pic' => $res['msg']);
                    break;
                default:
                    break;
            }
            $this->ajaxReturn($ret);
        }
        else{
            $this->ajaxReturn($res);
        }
    }







}